package com.techsoul.module.pms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ExcelException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.collection.MapUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.core.util.poi.ExcelExport;
import com.techsoul.common.core.util.poi.ExcelImport;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.pms.mapper.PmsBirdWatchSumMapper;
import com.techsoul.module.pms.pojo.entity.PmsBirdWatchSum;
import com.techsoul.module.pms.pojo.param.PmsBirdWatchSumSaveParam;
import com.techsoul.module.pms.pojo.param.PmsBirdWatchSumSearchParam;
import com.techsoul.module.pms.pojo.vo.PmsBirdWatchSumVo;
import com.techsoul.module.pms.service.IPmsBirdWatchSumService;
import com.techsoul.module.system.mapper.OperationLogMapper;
import com.techsoul.module.system.service.IConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 鸟类观测识别统计 Service实现
 *
 * @author liuen
 * @date 2021-12-08
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class PmsBirdWatchSumServiceImpl implements IPmsBirdWatchSumService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private IConfigService configService;

    @Autowired
    private PmsBirdWatchSumMapper mapper;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private OperationLogMapper operationLogMapper;
    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(PmsBirdWatchSumSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        PmsBirdWatchSum entity = new PmsBirdWatchSum();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "");
        entity.setCreateBy(sysUser.getId()) ;
        entity.setUpdateBy(sysUser.getId()) ;
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        this.mapper.insert(entity);
        return entity.getId();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(PmsBirdWatchSumSaveParam params) {
        PmsBirdWatchSum entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(sysUser.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public PmsBirdWatchSumVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<PmsBirdWatchSumVo> findAll(PmsBirdWatchSumSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<PmsBirdWatchSumVo> findAll(PmsBirdWatchSumSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }
    @Override
    public PmsBirdWatchSumVo removeSpecialChar(PmsBirdWatchSumVo input){

        Pattern p =null;
        Matcher m = null;;
        String rt = "";

        if(!StringUtils.isNull(input.getBirdName())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getBirdName());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setBirdName(rt);
        }
        if(!StringUtils.isNull(input.getCtDate())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCtDate());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCtDate(rt);
        }
        if(!StringUtils.isNull(input.getBirdCategory())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getBirdCategory());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setBirdCategory(rt);
        }

        if(!StringUtils.isNull(input.getCameraLoc())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCameraLoc());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCameraLoc(rt);
        }

        if(!StringUtils.isNull(input.getCameraId())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCameraId());
            rt = m.replaceAll("");
            rt=rt.trim();
            input.setCameraId(rt);
        }





        return input;
    }


    @Override
    public String exportData(PmsBirdWatchSumSearchParam params) {
        String baseDir = configService.findByConfigKey("project.attachment.baseDir");
        String baseUrl = configService.findByConfigKey("project.attachment.baseUrl");
        String fileName = "鸟类观测-"+params.getCtDate().replace("-", "")+ "-"+DateUtils.getDate("yyyyMMdd")+".xlsx";

        List<PmsBirdWatchSumVo> list = this.findAll(params);
        try {
            new ExcelExport(null, PmsBirdWatchSumVo.class).setDataList(list).writeFile(baseDir + fileName).close();
        } catch (Exception e) {
            log.error("鸟类观测", e);
            throw new ExcelException(e.getMessage());
        }
        return baseUrl + fileName;
    }

}
